package main;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import tools.Config;
import tools.GHTMLUtils;
import tools.GPrinterUtils;

public class Main {
	public static Logger logger = Logger.getLogger("GAdisyonLog");

	public static void main(String[] args) {
		// Phase 1 - Creating and Loading Config files
		// GHTMLUtils.createConfigFile();
		GHTMLUtils.loadConfigFile();
		FileHandler fh = null;
		String log = null;
		try {
			log = Config.LOG_FILE_DIRECTORY.getKey() + "adisyon.log";
			fh = new FileHandler(log);

			logger.addHandler(fh);
			logger.setLevel(Level.ALL);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
		} catch (SecurityException e) {
			logger.severe("Logging Error! \nProblem with: Security.");
			logger.fine(e.getMessage());
			GPrinterUtils.browseLog(log);
			e.printStackTrace();
		} catch (IOException e) {
			logger.severe("Logging Error! \nProblem with: Writing to file.");
			logger.fine(e.getMessage());
			GPrinterUtils.browseLog(log);
			e.printStackTrace();
		}
		// Phase 2 - Getting ID's from URL home
		String result;
		result = GHTMLUtils.getOrdersFromHTML(Config.URL_HOME.getKey());
		logger.fine("Returning result from getOrdersFromHTML :" + result);
		System.out.println("Returning result from getOrdersFromHTML :" + result);

		// Phase 3 - Parse order from target URL
		String[] results = GHTMLUtils.parseHTMLOrders(result);
		List<String> parsedLinks = new ArrayList<String>();
		for (String string : results) {
			logger.fine("Parsed links: " + Config.URL_TARGET.getKey() + string);
			System.out.println("Parsed links: " + Config.URL_TARGET.getKey() + string);
			parsedLinks.add(Config.URL_TARGET.getKey() + string);
		}
		String filePath = null;

		// Phase 4.0 - Convert HTML to PDF
		for (String urlString : results) {
			URI uri = null;
			try {
				uri = new URI(Config.URL_TARGET.getKey() + urlString);
				System.out.println(uri);
				GPrinterUtils.convertHTMLtoPDF(uri, Config.PDF_TARGET_DIRECTORY.getKey() + "job" + urlString + ".pdf");
				filePath = Config.PDF_TARGET_DIRECTORY.getKey() + "job" + urlString + ".pdf";
				System.out.println("PDF Created Succesfuly: " + filePath);
				logger.fine("PDF Created Succesfuly: " + filePath);
				filePath = null;
			} catch (URISyntaxException e) {
				System.out.println("URI Syntax Error!");
				logger.severe("URI Syntax Error! \nProblem with: " + uri);
				logger.fine(e.getMessage());
				GPrinterUtils.browseLog(log);
				e.printStackTrace();
			}
		}

		// // Phase 5 - Print PDF
		// PDDocument pdf;
		//
		// for (String pdfName : results) {
		// try {
		// filePath = Config.PDF_HOME_DIRECTORY.getKey() + "job" + pdfName +
		// ".pdf";
		// System.out.println(filePath);
		// pdf = PDDocument.load(filePath);
		// pdf.silentPrint();
		// pdf.close();
		// System.out.println("Printed Successfuly: " + filePath);
		// logger.fine("Printed Successfuly: " + filePath);
		// filePath = null;
		// } catch (IOException e) {
		// System.out.println("Pdf Error!");
		// logger.severe("Pdf Error! \nProblem with: " + filePath);
		// logger.fine(e.getMessage());
		// e.printStackTrace();
		// } catch (PrinterException e) {
		// System.out.println("Printer Error!");
		// PrintService service = GPrinterUtils.getDefaultPrinter();
		// logger.severe("Printer Error! \nProblem with: " + service.getName());
		// logger.fine(e.getMessage());
		// GPrinterUtils.browseLog(log);
		// e.printStackTrace();
		// }
		// }
		// Phase 6 - Logging into file
		logger.info("Logging completed");
		fh.close();
	}
}
